package com.lanou.controller;

import com.lanou.service.LoginService;
import com.lanou.utils.JsonResult;
import com.lanou.utils.StringUtils;
import com.lanou.utils.code.VerifyCode;
import com.lanou.utils.login.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Api(value = "登录",tags = "登录")
@RestController
public class LoginController {
@Autowired
private LoginService loginService;
@ApiOperation(value = "生成验证码",httpMethod = "POST")
@RequestMapping("/login")
public JsonResult login(@RequestBody LoginUser loginUser, HttpSession session){
System.out.println(loginUser.getUsername());
System.out.println(loginUser.getPassword());
System.out.println(loginUser.getLoginCode());
if (StringUtils.isEmpty(loginUser.getUsername())){
return new JsonResult(400,"登陆失败","用户名为空");
}
if (StringUtils.isEmpty(loginUser.getPassword())){
return new JsonResult(400,"登陆失败","密码为空");
}
String code = VerifyCode.getInstance().getCode();
System.out.println(code);
if (StringUtils.isEmpty(code) ||
StringUtils.isEmpty(loginUser.getLoginCode()) ||
!loginUser.getLoginCode().equals(code)){
return new JsonResult(400,"登陆失败","验证码不对");
}
boolean login =
loginService.login(loginUser.getUsername(),
loginUser.getPassword());
return login ? new JsonResult(200,"登录成功") : new
JsonResult(400,"登录失败","系统繁忙，请稍后再试");
}
}